package com.euq.controller;

import com.euq.dto.MenuDto;
import com.euq.entity.Menu;
import com.euq.log.BusinessLog;
import com.euq.log.Log;
import com.euq.service.MenuService;
import com.euq.utils.SecurityUtils;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    private static final Logger log = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    private MenuService menuService;
    @GetMapping("/list")
    public PageInfo<Menu> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size){
        return menuService.list(page,size);
    }
    @GetMapping("/tree")
    public ResponseEntity<?> getMenuTree() {
        List<MenuDto> treeNodes = menuService.getMenuTreeNodes();
        return ResponseEntity.ok(treeNodes);
    }
    @PostMapping("/delete")
    @BusinessLog(logName = "删除菜单",logType = "业务日志")
    public void delete(@RequestParam Long id) {
        log.info("用户{}删除菜单", SecurityUtils.getCurrentUser().getUsername());
        menuService.delete(id);
    }
    @PostMapping("/save")
    @BusinessLog(logName = "编辑菜单",logType = "业务日志")
    public void save(@RequestBody@Valid Menu menu) {
        log.info("用户{}编辑{}菜单信息",SecurityUtils.getCurrentUser().getUsername(),menu.getName());
        menuService.save(menu);
    }
}
